/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.core.rtgen.managergenerator.mgrImplclass.methodgenerators.getvonodecode;

import com.inspur.edp.bff.api.attribute.VMRootAttribute;
import com.inspur.edp.bff.designtime.generator.core.rtgen.managergenerator.mgrImplclass.FSMgrImplGenContext;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.method.ClassMethodGeneratorContext;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.repository.common.CefDataInterfaceInfo;
import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class GetVoNodeCodeMethodGenerator extends ClassMethodGenerator {
    private final String paramFrontNodeCode = "frontNodeCode";
    private FSMgrImplGenContext mgrImplGenContext;
    private GspViewModel vo;

    public GetVoNodeCodeMethodGenerator(FSMgrImplGenContext mgrImplGenContext) {
        this.mgrImplGenContext = mgrImplGenContext;
        vo = mgrImplGenContext.getMgrGenContext().getFSGeneratorContext().getVmGenContext().getVm();
    }

    @Override
    protected ClassMethodGeneratorContext createClassMethodInfoContext() {
        return new GetVoNodeCodeMethodGenCtx(mgrImplGenContext);
    }

    @Override
    protected Block buildMethodBody() {
        Block block = ast.newBlock();
        SwitchStatement switchStatement = ast.newSwitchStatement();
        switchStatement.setExpression(ast.newSimpleName(paramFrontNodeCode));

//        makeCaseByRootAttr(rootAttr, switchStatement);
        makeCaseByPropertyAttr(switchStatement);
        makeDefaultCase(switchStatement);
        block.statements().add(switchStatement);
        return block;
    }

    private void makeDefaultCase(SwitchStatement switchStatement) {
        SwitchCase switchCase = ast.newSwitchCase();
        switchCase.setExpression(null);
        switchStatement.statements().add(switchCase);
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ast.newNullLiteral());
        switchStatement.statements().add(returnStatement);
    }

    private void makeCaseByPropertyAttr(SwitchStatement switchStatement) {
//        Method[] interfaceProperties = TargetInterfaceType.getMethods();
//        List<String> objects = new ArrayList<String>();
        for (IGspCommonObject object : vo.getAllObjectList()) {


            if (!object.getCode().equals(StringUtils.toCamelCase(object.getCode()))) {
                getObejectTwoCases(object.getCode(), switchStatement);
            } else {
                getObjectOneCase(object.getCode(), switchStatement);
            }
        }
    }

//    private HashMap<String, CefDataInterfaceInfo> getChildInterfaceType() {
//        return mgrImplGenContext.getMgrGenContext().getFSGeneratorContext().getEntityContext().getChildInterfaceTypes();
//    }
//
//    private Class getInterfaceType() {
//        return mgrImplGenContext.getMgrGenContext().getFSGeneratorContext().getCurrentTypeInfo().getEntityEntityTypeInfo().getDataInterfaceType();
//    }
//
//    private void makeCaseByRootAttr(VMRootAttribute rootAttr, SwitchStatement switchStatement) {
//        if (rootAttr == null)
//            return;
//        if (!rootAttr.objectCode().equals(StringUtils.toCamelCase(rootAttr.objectCode()))) {
//            getObejectTwoCases(rootAttr.objectCode(), switchStatement);
//        } else {
//            getObjectOneCase(rootAttr.objectCode(), switchStatement);
//        }
//
//    }

    private void getObjectOneCase(String objectCode, SwitchStatement switchStatement) {
        SwitchCase switchCase = ast.newSwitchCase();
        switchCase.setExpression(GeneratorUtil.getStringLiteral(ast, objectCode));
        switchStatement.statements().add(switchCase);
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(GeneratorUtil.getStringLiteral(ast, objectCode));
        switchStatement.statements().add(returnStatement);
    }

    private void getObejectTwoCases(String objectCode, SwitchStatement switchStatement) {
        SwitchCase switchCase = ast.newSwitchCase();
        switchCase.setExpression(GeneratorUtil.getStringLiteral(ast, objectCode));
        switchStatement.statements().add(switchCase);
        SwitchCase switchCase1 = ast.newSwitchCase();
        switchCase1.setExpression(GeneratorUtil.getStringLiteral(ast, StringUtils.toCamelCase(objectCode)));
        switchStatement.statements().add(switchCase1);
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(GeneratorUtil.getStringLiteral(ast, objectCode));
        switchStatement.statements().add(returnStatement);

    }

//    private Class getInterFaceIype() {
//        return mgrImplGenContext.getMgrGenContext().getFSGeneratorContext().getCurrentTypeInfo().getEntityEntityTypeInfo().getDataInterfaceType();
//    }

    @Override
    protected String getMethodName() {
        return "getVoNodeCode";
    }

    @Override
    protected TypeInfo getReturnType() {
        return new TypeInfo(String.class);
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        return new ArrayList<>(Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        ParameterInfo parameterInfo = new ParameterInfo();
        parameterInfo.setParamName(paramFrontNodeCode);
        parameterInfo.setParamType(new TypeInfo(String.class));
        return new ArrayList<>(Arrays.asList(new ParameterInfo[]{parameterInfo}));
    }

//    private VMRootAttribute getVmRootAttr() {
//        return (VMRootAttribute) getInterFaceIype().getAnnotation(VMRootAttribute.class);
//    }
}
